package common

import (
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/models/base"
)

const (
	TABLE_ADMIN_OPERATE_LOG = "admin_operate_log"
)

type AdminOperateLog struct {
	Id          uint64 `json:"id" xml:"id"`
	AdminId     uint64 `json:"admin_id" xml:"admin_id"`
	AdminName   string `json:"admin_name" xml:"admin_name"`
	Router      string `json:"router" xml:"router"`
	OperateDesc string `json:"operate_desc" xml:"operate_desc"`
	OperateIp   uint64 `json:"operate_ip" xml:"operate_ip"`
	AddTime     string `json:"add_time" xml:"add_time"`
}

/**
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminOperateLogMap2Model(data map[string]string) AdminOperateLog {
	return AdminOperateLog{
		Id:          uint64(helper.ToInt(data["id"])),
		AdminId:     uint64(helper.ToInt(data["admin_id"])),
		AdminName:   data["admin_name"],
		Router:      data["router"],
		OperateDesc: data["operate_desc"],
		OperateIp:   uint64(helper.ToInt(data["operate_ip"])),
		AddTime:     data["add_time"],
	}
}

/**restful新增数据
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminOperateLogCreate(ctx *gin.Context, info map[string]interface{}) uint64 {
	id := base.NewCommonModel(ctx).Create(TABLE_ADMIN_OPERATE_LOG, info)
	return uint64(id)
}

/**restful删除，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminOperateLogDelete(ctx *gin.Context, id uint64) int64 {
	return base.NewCommonModel(ctx).Delete(TABLE_ADMIN_OPERATE_LOG, helper.ToString(id))
}

/**restful详情，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminOperateLogInfo(ctx *gin.Context, where map[string]interface{}) AdminOperateLog {
	info := base.NewCommonModel(ctx).Info(TABLE_ADMIN_OPERATE_LOG, where)
	if info != nil {
		return AdminOperateLogMap2Model(info)
	}
	return AdminOperateLog{}
}

/**restful列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminOperateLogIndex(ctx *gin.Context, page int64, pageSize int64, where map[string]interface{}, orders []string) map[string]interface{} {
	result := base.NewCommonModel(ctx).Index(TABLE_ADMIN_OPERATE_LOG, page, pageSize, where, orders)

	if len(result["list"].([]map[string]string)) > 0 {
		list := []AdminOperateLog{}
		for _, data := range result["list"].([]map[string]string) {
			list = append(list, AdminOperateLogMap2Model(data))
		}
		result["list"] = list
	}

	return result
}
